<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
    protected $table = "cate";
    protected $primaryKey = 'cid';    
    public $timestamps = false;
    protected $guarded = [];

    //树状图（无限极分类）
    public function getTree($data,&$result=array(),$spac=-4,$pid=0)
    {
        $spac = $spac+4;

        foreach($data as $k=>$v){
            if($v->pid==$pid){
                $v['_cname']=str_repeat('&nbsp;',$spac).'|--'.$v['cname'];
                $result[]=$v;
                $this->getTree($data,$result,$spac,$data[$k]['cid']);

            }

        }
        return $result;
    }
    //关联
    //分类与类型关联
    public function cate_type(){
        return $this->belongsTo('App\Http\Model\Type','type_tid','tid');
    }
    //分类与类型属性关联


    //分配当前分类所在的顶级分类的所有子分类
    public function parents_sons($data,$pid){
        //查顶级分类
        $temp=[];
        foreach ($data as $v){

            if($v->cid==$pid){
//                dd($v->cid);
                $temp[]=$v->cid;
                $temp = array_merge($temp,$this->parents_sons($data, $v->pid));
            }
        }

        return $temp;
    }

    //获得非自己和子集的分类
    public function get_selfs($cid){
        $data = $this->get();
        $son = $this->get_son($data,$cid);
        $son[]=$cid;
        $where="cid IN (".implode(',',$son).")";
        
        $array_myselfs = $this->where($where)->get();
        return $array_myselfs;
        }

//    获得某个分类的子集
//    public function get_son($data,$cid){
//        $temp=[];
//        foreach($data as $dt){
//            if($dt['pid']==$cid){
//                $temp[] = $dt['cid'];
//                $temp = array_merge($temp,$this->get_son($data,$dt['cid']));
//            }
//
//        }
//        return $temp;
//    }



    
}
